package chat.ui;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;


public class TabbedPanel extends JTabbedPane{
	
	public TabbedPanel(){
		
		setTabPlacement(JTabbedPane.TOP);
		
	}
	
	public void addNewTab(String tab_name){
		
		
		
		boolean flag = false;
		
		JEditorPane text_area = new JEditorPane();
		JScrollPane scroll = new JScrollPane(text_area);
		text_area.setEditable(false);
		
		for (int i=0; i<this.getTabCount(); i++){
			if (this.getTitleAt(i).equalsIgnoreCase(tab_name)){
				flag = true;
			}
		}
		if (flag){}
		else {
			this.addTab(tab_name, scroll);
			}
	}
	
	public JEditorPane getTextArea(String tab_name){
		
		JEditorPane area = null;
		JScrollPane text = null;
		
		for (int i=0; i<this.getTabCount(); i++){
			String title = this.getTitleAt(i);
			if (title.equalsIgnoreCase(tab_name)){
				text = (JScrollPane) this.getComponentAt(i);
				area = (JEditorPane) text.getViewport().getComponent(0);
				break;
			}
		}
		return area;
	}
	
	public void removeTextArea (String group_name){
		for (int i=0; i<this.getTabCount(); i++){
			String title = this.getTitleAt(i);
			if (title.equalsIgnoreCase(group_name)){
				this.remove(i);
			}
		}
	}

	public JEditorPane getUserTextArea (String user_name){
		
		JEditorPane text_area = this.getTextArea(user_name);
		
		if (text_area!=null){
			return text_area;
		}
		else {
			addNewTab(user_name);
			text_area = this.getTextArea(user_name);
			return text_area;
		}
	
	}
	
	
	
}
